package com.guit.hrms.service.impl;

import com.guit.hrms.po.Article;
import com.guit.hrms.mapper.ArticleMapper;
import com.guit.hrms.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    
    @Override
    public List<Article> getAllArticles() {
        log.info("开始查询所有文章");
        List<Article> articles = articleMapper.findAll();
        log.info("查询到 {} 篇文章", articles.size());
        return articles;
    }

    @Override
    public List <Article> getAllArticlesWithToken(){
        log.info("管理员开始查询所有文章");
        List<Article> articles = articleMapper.findAllWithToken();
        log.info("管理员查询到 {} 篇文章", articles.size());
        return articles;
    }

    @Override
    public Article getArticleById(Long id) {
        return articleMapper.findById(id);
    }
    
    @Override
    public List<Article> getArticlesByType(Integer type) {
        return articleMapper.findByType(type);
    }
    
    @Override
    @Transactional
    public Article createArticle(Article article) {
        try {
            log.info("开始保存文章: {}", article);
            
            // 设置必要的字段
            article.setPublishDate(LocalDateTime.now());
            article.setViewCount(0);
            article.setStatus(true);
            article.setHasAttachment(article.getHasAttachment() != null ? article.getHasAttachment() : false);
            
            // 插入数据
            int rows = articleMapper.insert(article);
            log.info("插入结果: {} rows affected", rows);
            
            if (rows == 0) {
                throw new RuntimeException("数据库插入失败");
            }
            
            log.info("文章创建成功，ID: {}", article.getId());
            return article;
        } catch (Exception e) {
            log.error("创建文章异常", e);
            throw new RuntimeException("创建文章失败: " + e.getMessage());
        }
    }
    
    @Override
    @Transactional
    public Article updateArticle(Article article) {
        Article existing = articleMapper.findById(article.getId());
        if (existing == null) {
            throw new RuntimeException("文章不存在");
        }
        
        int rows = articleMapper.update(article);
        if (rows == 0) {
            throw new RuntimeException("更新文章失败");
        }
        
        return articleMapper.findById(article.getId());
    }
    
    @Override
    @Transactional
    public boolean deleteArticle(Long id) {
        return articleMapper.delete(id) > 0;
    }

    @Override
    public int restoreArticle(Long id) {
        return articleMapper.updateStatus(id);
    }

    @Override
    public void incrementViewCount(Long id) {
        articleMapper.incrementViewCount(id);
    }
    
    @Override
    @Transactional
    public boolean hardDeleteArticle(Long id) {
        try {
            log.info("开始硬删除文章，ID: {}", id);
            
            // 先删除文章关联的附件
            int attachmentRows = articleMapper.deleteAttachments(id);
            log.info("删除附件结果: {} 个附件被删除", attachmentRows);
            
            // 再删除文章
            int articleRows = articleMapper.hardDelete(id);
            log.info("删除文章结果: {} 篇文章被删除", articleRows);
            
            if (articleRows == 0) {
                throw new RuntimeException("文章不存在或删除失败");
            }
            
            return true;
        } catch (Exception e) {
            log.error("硬删除文章失败", e);
            throw new RuntimeException("删除文章失败: " + e.getMessage());
        }
    }
} 